﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using AppData;

public class TextureSaver
{
    #region User
    public static void DeleteUserTexture(long bookId, int texId)
    {
        DeleteTexture(ServerConfig.userTextureDir, bookId, texId.ToString());
    }
    public static int SaveUserTexture(long bookId, int texId, /*int texWidth, int texHeight,*/ byte[] tex)
    {
        return SaveTexture(ServerConfig.userTextureDir, bookId, texId.ToString(), /*texWidth, texHeight,*/ tex);
    }
    public static Tex LoadUserTexture(long bookId, int texId)
    {
        return LoadTexture(ServerConfig.userTextureDir, bookId, texId.ToString());
    }
    #endregion

    #region book

    static string GetBookTexDir(string genre)
    {
        return ServerConfig.bookTextureDir + "/" + genre.ToString();
    }
    public static void DeleteBookTexture(long bookId, string genre)
    {
        DeleteTexture(GetBookTexDir(genre), bookId, "");
    }
    public static int SaveBookTexture(long bookId, string genre, /*int texWidth, int texHeight, */byte[] tex)
    {
        return SaveTexture(GetBookTexDir(genre), bookId, "",/* texWidth, texHeight,*/ tex);
    }
    public static Tex LoadBookTexture(long bookId, string genre)
    {
        return LoadTexture(GetBookTexDir(genre), bookId, "");
    }
    #endregion

    #region moment
    public static void DeleteMomentPhoto(long bookId, int texId)
    {
        DeleteTexture(ServerConfig.momentPhotosDir, bookId, texId.ToString());
    }
    public static int SaveMomentPhoto(long bookId,long momentId, int texId, Tex tex)
    {
        return SaveMomentPhoto(bookId, momentId, texId, tex.tex);
    }
    public static int SaveMomentPhoto(long bookId, long momentId, int texId, byte[] tex)
    {
        return SaveTexture(ServerConfig.momentPhotosDir, bookId, momentId+"_"+texId,  tex);
    }
    public static Tex LoadMomentPhoto(long bookId, long momentId,int texId)
    {
        return LoadTexture(ServerConfig.momentPhotosDir, bookId, momentId + "_" +texId);
    }
    #endregion

    public static void DeleteTexture(string texDir, long bookId, string texId)
    {
        string texFilePath = GetTexFilePath(texDir, bookId, texId);
        if (File.Exists(texFilePath))
            File.Delete(texFilePath);
        string infoFilePath = GetInfoFilePath(texDir, bookId, texId);
        if (File.Exists(infoFilePath))
            File.Delete(infoFilePath);
    }
    public static int SaveTexture(string texDir, long bookId, string texId, /*int texWidth, int texHeight,*/ byte[] tex)
    {
        try
        {
            if (!Directory.Exists(texDir))
            {
                GameDebug.Log($"create texture dir:{texDir}");
                Directory.CreateDirectory(texDir);
            }
            string texFilePath = GetTexFilePath(texDir,bookId, texId);
            //string infoFilePath = GetInfoFilePath(texDir,bookId, texId);

            //string info = GetInfo(texWidth, texHeight);

            File.WriteAllBytes(texFilePath, tex);
            //File.WriteAllText(infoFilePath, info);
            return 0;
        }
        catch(Exception ex)
        {
            GameDebug.LogException(ex);
            return -1;
        }
    }
    public static Tex LoadTexture(string texDir,long bookId, string texId)
    {
        string texFile = GetTexFilePath(texDir,bookId, texId);
        if (File.Exists(texFile))
        {
            Tex tex = new Tex();
            tex.tex = File.ReadAllBytes(texFile);

            //LoadInfo(texDir,bookId, texId, out int texWidth, out int texHeight);

            //tex.texWidth = texWidth;
            //tex.texHeight = texHeight;

            return tex;
        }
        return null;
    }

    public static string GetInfo(int texWidth, int texHeight)
    {
        return texWidth + "_" + texHeight;
    }

    public static void LoadInfo(string texDir, long bookId, string texId,out int texWidth,out int texHeight)
    {
        texWidth = texHeight = 0;

        string infoFilePath = GetInfoFilePath(texDir,bookId, texId);
        if (File.Exists(infoFilePath))
        {
            string info = File.ReadAllText(infoFilePath);
            string[] strs = info.Split('_');
            if (strs.Length == 2)
            {
                int.TryParse(strs[0], out texWidth);
                int.TryParse(strs[1], out texHeight);
            }
        }
    }

    public static string GetTexFilePath(string texDir,long bookId, string texId)
    {
        string fileName = bookId + "_" + texId + ".jpg";
        string filePath = texDir + "/" + fileName;// ServerConfig.userTextureDir + "/" + fileName;
        return filePath;
    }
    public static string GetInfoFilePath(string texDir, long bookId, string texId)
    {
        string fileName = bookId + "_" + texId + ".txt";
        string filePath = texDir + "/" + fileName; ;// ServerConfig.userTextureDir + "/" + fileName;
        return filePath;
    }
}

